<script setup>
import {reactive, ref, onMounted} from 'vue';
import {useRouter, useRoute} from 'vue-router';
import {ElMessage} from 'element-plus';
import {useUserStore} from '@/stores/user/user.js';
import router from "@/router/index.js";

const route = useRoute();
const userStore = useUserStore();

const loginFormRef = ref(null);
const loading = ref(false);

const loginForm = reactive({
  username: '',
  password: '',
});

const loginRules = reactive({
  username: [
    {required: true, message: "用户名不能为空", trigger: ["blur", "change"]},
    {min: 3, max: 12, message: "用户名长度需为3-12位", trigger: ["blur", "change"]}
  ],
  password: [
    {required: true, message: "密码不能为空", trigger: ["blur", "change"]},
    {pattern: /^[a-zA-Z0-9]{6,18}$/, message: "密码需为6-18位字母或数字"}
  ]
});

const submitForm = async () => {
  if (!loginFormRef.value) return;
  await loginFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      userStore
          .login(loginForm)
          .then(() => {
            ElMessage.success({message: '登录成功', duration: 3000});
            //  在此处添加路由跳转
            let redirect = ''
            if (userStore.role === 'user') {
              redirect = route.query?.redirect || '/user/home';
            } else {
              redirect = route.query?.redirect || '/admin/dashboard';
            }
            router.push(redirect);
          })
          .catch((error) => {
            console.log('登录错误详情:', error.message);
            ElMessage.error({message: error.message || '未知错误', duration: 3000});
          })
          .finally(() => {
            loading.value = false;
          });
    } else {
      console.log('验证失败');
      return false;
    }
  });
};
</script>

<template>
  <div class="login-container">
    <el-card class="login-card">
      <h1 style="text-align: center">欢迎登录</h1>
      <el-form
          :model="loginForm"
          :rules="loginRules"
          ref="loginFormRef"
          label-width="0px"
          class="login-form"
      >
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              prefix-icon="User"
              placeholder="用户名"
              class="input-width"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              prefix-icon="Lock"
              type="password"
              placeholder="密码"
              show-password
              @keyup.enter="submitForm"
              class="input-width"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" :loading="loading" class="login-button">
            登录
          </el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.login-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #209cff 0%, #68e0cf 100%);

}

.login-card {
  width: 450px;
  height: 300px;
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
  justify-content: center;
  flex-direction: column;
  align-items: center;

}

.login-form {
  margin-top: 20px;
  width: 100%;
}

.input-width {
  width: 300px;
  margin: 20px auto 0;
}

.login-button {
  margin: 20px auto 0;
}

.el-form-item {
  display: flex;
  justify-content: center;
}

::v-deep .el-form-item__error {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 300px; /* 与输入框同宽 */
  padding-left: 40px; /* 对齐输入框内容起始位置 */
  text-align: left;
  margin-top: 4px;
}
</style>



